package cn.jetpiece.cloud.joss.sdk.core.oss;

import cn.jetpiece.cloud.joss.sdk.core.oss.model.CreateAccessor;
import cn.jetpiece.cloud.joss.sdk.core.oss.model.UpdateAccessor;
import cn.jetpiece.cloud.joss.sdk.core.util.RestUtils;
import com.alibaba.fastjson.JSONObject;
import cn.jetpiece.cloud.joss.sdk.core.oss.model.OssSdkBaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

/**
 * @author XuJZ
 * @date 2021/1/28
 */
public class AccessService {
    private RestUtils restUtils;
    private final Logger log = LoggerFactory.getLogger(AccessService.class);

    public void setRestUtils(RestUtils restUtils) {
        this.restUtils = restUtils;
    }

    /**
     * @param accessorName      桶名
     * @param defaultBucketName 默认桶名称
     */
    public void createAccessor(String accessorName, String defaultBucketName) {
        CreateAccessor create = new CreateAccessor();
        create.setAccessorName(accessorName);
        create.setDefaultBucketName(defaultBucketName);
        createAccessor(create);
    }

    /**
     * @param request 桶信息
     */
    public void createAccessor(CreateAccessor request) {
        Assert.notNull(request.getAccessorName(), "系统名称不能为空！");
        Assert.notNull(request.getDefaultBucketName(), "默认桶名称不能为空！");
        OssSdkBaseResult<String> baseResult = restUtils.exchange("/oss/accessors", HttpMethod.POST, request, String.class);
        log.info(JSONObject.toJSONString(baseResult));
    }

    /**
     * @param request 桶信息
     */
    public void updateAccessor(UpdateAccessor request) {
        Assert.notNull(request.getId(), "UUID不能为空！");
        OssSdkBaseResult<String> baseResult = restUtils.exchange("/oss/accessors", HttpMethod.PUT, request, String.class);
        log.info(JSONObject.toJSONString(baseResult));
    }

    /**
     *
     */
    public void getBucket() {
        OssSdkBaseResult<String> baseResult = restUtils.exchange("/oss/buckets", HttpMethod.GET, String.class);
        log.info(JSONObject.toJSONString(baseResult));
    }

}
